<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Random;
use think\Db;
use think\Exception;
use think\Hook;
use app\common\library\Token;
/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class User extends Backend
{
    
    /**
     * User模型对象
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $_user = 'yry_user';
    protected $_token = '';
    //Token默认有效时长
    protected $keeptime = 2592000;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\User;
        $this->view->assign("genderList", $this->model->getGenderList());
        $this->view->assign("typeList", $this->model->getTypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['id','username','mobile','nickname','avatar','gender','idcard','age','type']);
                
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    public function add(){
        if ($this->request->isPost()) {
            $mobile = $this->request->request("mobile");
            $password = $this->request->request("password");
            if (!$mobile || !$password) {
                $this->error('缺少参数');
            }
            $result = db("user")->field("id")->where('mobile', $mobile)->find();
            if ($result) {
                $this->error("用户已存在");
            }
            $time = time();
            $ip = request()->ip();
            $data = [
                'username' => $mobile,
                'password' => $password,
                'mobile'   => $mobile
            ];
            $params = array_merge($data, [
                'salt'      => Random::alnum(),
                'jointime'  => $time,
                'joinip'    => $ip,
                'logintime' => $time,
                'loginip'   => $ip,
                'prevtime'  => $time,
                'status'    => 'normal'
            ]);
            $auth = new \app\common\library\Auth;
            $params['password'] = $auth->getEncryptPassword($password, $params['salt']);
            //账号注册时需要开启事务,避免出现垃圾数据
            Db::startTrans();
            try {
                $userid = db('user')->insertGetId($params);
                //设置Token
                $this->_token = Random::uuid();
                Token::set($this->_token, $userid, $this->keeptime);
                //注册成功的事件
                Hook::listen("user_register_successed", $this->_user, $data);
                Db::commit();
                $this->success("注册成功");
            } catch (Exception $e) {
                Db::rollback();
                $this->error("注册失败");
            }
        }
        return $this->view->fetch();
    }












}
